
import React, { useEffect } from 'react';
import { BrowserRouter, Route, Redirect, Switch } from 'react-router-dom';
import CitySelect from './pages/CitySelect';
import Index from './pages/Index';
import MapFound from './pages/Found';
import PageNotFound from './pages/PageNotFound';

import { getCityByBaiduMap } from './utils/baiduMap'
import { useDispatch, useSelector } from 'react-redux';
import { GET_CITY } from './constans/types'

const App = () => {
  const cityName = useSelector(state => state.mapReducer.cityName)
  const dispatch = useDispatch()

  const getCity = () => {
    getCityByBaiduMap()
      .then(res => {
        dispatch({ type: GET_CITY, value: res.replace('市', '') });
      });
  };
  useEffect(() => { getCity() }, [getCity])


  return <BrowserRouter>
    <Switch>
      <Route path='/home' component={Index} />
      <Route path='/map' >{cityName !== '获取中' && <MapFound />}
      </Route>
      <Route path='/citylist' component={CitySelect} />
      <Route path='/404' component={PageNotFound} />
      <Redirect exact from="/" to="/home"></Redirect>
      <Redirect from="*" to="/404" ></Redirect>
    </Switch></BrowserRouter>

}


export default App
